package com.study.talk.data.repository

import android.util.Log
import com.study.talk.data.model.resp.ResultResponse
import retrofit2.Response

open class BaseRepository {
    fun <T> handleResponse(
        name: String,
        response: Response<ResultResponse<T>>,
        success: (T) -> Unit,
        fail: (String) -> Unit
    ): T? {
        if (response.isSuccessful) {
            if (response.body()?.status == 200) {
                if (response.body()?.data == null) {
                    Log.e(name, "${name}: response.body()?.data is null")
                    fail("response.body()?.data is null")
                    return null
                } else {
                    success(response.body()?.data!!)
                    return response.body()?.data!!
                }
            } else {
                response.body()?.message?.let { fail(it) }
                return null
            }
        } else {
            Log.e(name, "request fail,msg:${response.message()}")
            fail(response.message())
            return null
        }
    }
}